/*
ID: pallab81
PROG:
LANG: C++
*/

//"I have not failed, I have just found 10000 ways that won't work.

#include <iostream>
#include <algorithm>
#include <vector>
#include <sstream>
#include <fstream>
#include <string>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <functional>
#include <bitset>
#include <iomanip>

#include <ctime>
#include <cassert>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <climits>
#include <cstdlib>

using namespace std;

#define VI vector< int >
#define CI( x ) scanf("%d",&x)
#define CL( x ) scanf("%lld",&x)
#define CD( x ) scanf("%lf",&x )
#define foR(i1,st,ed) for(int i1 = st,j1 = ed ; i1 < j1 ; ++i1 )
#define fo(i,ed) foR( i , 0 , ed )
#define foE(i,st,ed) foR( i , st , ed+1 )
#define foit(i, x) for (typeof((x).begin()) i = (x).begin(); i != (x).end(); i++)
#define bip( X ) sleep( X )
#define Int long long
#define pb push_back
#define SZ(X) (int)(X).size()
#define LN(X) (int)(X).length()
#define mk make_pair
#define f first
#define s second
#define SET( ARRAY , VALUE ) memset( ARRAY , VALUE , sizeof(ARRAY) )

class MonsterFarm {
public:
    int numMonsters(vector <string> transforms);
};
const int maxn = 60;
int N;
vector< int > E[ maxn ];
inline void split(vector< string > &vi ) {
    fo(i,N) {
        E[i].clear();
    }

    fo(i,N) {
        stringstream ss(vi[i]);
        for (int j; ss>>j; ) {
            E[i].pb(j-1);
        }
    }
}
int used[maxn][maxn];
bitset< maxn > cycle;
bitset< maxn > seen;
void dfs(int u, int p) {
    used[p][u]=1;
    fo(i,SZ(E[u])) {
        int v = E[u][i];
        if ( !used[p][v] ) {
            dfs( v,p );
        }
    }

}

inline bool toinfite(int u) {
    seen=0;
    // where I can go from u
    queue<int> q;
    q.push( u );
    seen[u]=1;

    while ( !q.empty() ) {
        u = q.front();
        q.pop();
        fo(i,SZ(E[u])) {
            int v = E[u][i];
            if ( !seen[v] ) {
                q.push(v);
                seen[v]=1;
            }
        }
    }
    fo(i,N) {
        if ( seen[i] && cycle[i] && SZ(E[i])>1 ) {
            return true;
        }
    }
    return false;
}
const Int mod = 1000000007;
Int dp[maxn];

Int go( int u ) {
    if ( seen[u] ) {
        return dp[u];
    }
    seen[u]=1;
    Int res=0;
    if ( cycle[u] ) {
        return dp[u]=res=1;
    }

    fo(i,SZ(E[u])) {
        int v = E[u][i];
        res = (res + go(v) )%mod;
    }

    return dp[u]=res;
}
int MonsterFarm :: numMonsters(vector <string> transforms) {
    N = SZ( transforms );
    split( transforms);

    SET(used,0);
    fo(i,N) {
        dfs(i,i);
    }
    cycle=0;
    fo(u,N) {
        if ( cycle[u] )continue;
        fo(j,SZ(E[u])) {
            int v = E[u][j];
            if ( used[v][u] && used[v][u] ) {
                cycle[u]=1;
                break;
            }
        }
    }
//     fo(i,N) {
//         cout<<i<<" -> "<<SZ(E[i])<<" = > ";
//         puts( cycle[i] ? "CYCLE" : "NO CYCLE" );
//     }

    if ( toinfite(0) ) {
        return -1;
    }

    seen=0;
    Int var = go(0);
    return var;
}
// BEGIN KAWIGIEDIT TESTING
// Generated by KawigiEdit 2.1.8 (beta) modified by pivanof
#include <iostream>
#include <string>
#include <vector>
using namespace std;
bool KawigiEdit_RunTest(int testNum, vector <string> p0, bool hasAnswer, int p1) {
    cout << "Test " << testNum << ": [" << "{";
    for (int i = 0; int(p0.size()) > i; ++i) {
        if (i > 0) {
            cout << ",";
        }
        cout << "\"" << p0[i] << "\"";
    }
    cout << "}";
    cout << "]" << endl;
    MonsterFarm *obj;
    int answer;
    obj = new MonsterFarm();
    clock_t startTime = clock();
    answer = obj->numMonsters(p0);
    clock_t endTime = clock();
    delete obj;
    bool res;
    res = true;
    cout << "Time: " << double(endTime - startTime) / CLOCKS_PER_SEC << " seconds" << endl;
    if (hasAnswer) {
        cout << "Desired answer:" << endl;
        cout << "\t" << p1 << endl;
    }
    cout << "Your answer:" << endl;
    cout << "\t" << answer << endl;
    if (hasAnswer) {
        res = answer == p1;
    }
    if (!res) {
        cout << "DOESN'T MATCH!!!!" << endl;
    } else if (double(endTime - startTime) / CLOCKS_PER_SEC >= 2) {
        cout << "FAIL the timeout" << endl;
        res = false;
    } else if (hasAnswer) {
        cout << "Match :-)" << endl;
    } else {
        cout << "OK, but is it right?" << endl;
    }
    cout << "" << endl;
    return res;
}
int main() {
    bool all_right;
    all_right = true;

    vector <string> p0;
    int p1;

    {
        // ----- test 0 -----
        string t0[] = {"1"};
        p0.assign(t0, t0 + sizeof(t0) / sizeof(t0[0]));
        p1 = 1;
        all_right = KawigiEdit_RunTest(0, p0, true, p1) && all_right;
        // ------------------
    }

    {
        // ----- test 1 -----
        string t0[] = {"1 1"};
        p0.assign(t0, t0 + sizeof(t0) / sizeof(t0[0]));
        p1 = -1;
        all_right = KawigiEdit_RunTest(1, p0, true, p1) && all_right;
        // ------------------
    }

    {
        // ----- test 2 -----
        string t0[] = {"2","3","1"};
        p0.assign(t0, t0 + sizeof(t0) / sizeof(t0[0]));
        p1 = 1;
        all_right = KawigiEdit_RunTest(2, p0, true, p1) && all_right;
        // ------------------
    }

    {
        // ----- test 3 -----
        string t0[] = {"1","3 4","2","2"};
        p0.assign(t0, t0 + sizeof(t0) / sizeof(t0[0]));
        p1 = 1;
        all_right = KawigiEdit_RunTest(3, p0, true, p1) && all_right;
        // ------------------
    }

    {
        // ----- test 4 -----
        string t0[] = {"2 2","3","4 4 4","5","6","7 7 7 7","7"};
        p0.assign(t0, t0 + sizeof(t0) / sizeof(t0[0]));
        p1 = 24;
        all_right = KawigiEdit_RunTest(4, p0, true, p1) && all_right;
        // ------------------
    }

    {
        // ----- test 5 -----
        string t0[] = {"2 3","5 7","2 4","5","6","4","7"};
        p0.assign(t0, t0 + sizeof(t0) / sizeof(t0[0]));
        p1 = 5;
        all_right = KawigiEdit_RunTest(5, p0, true, p1) && all_right;
        // ------------------
    }
    {
        // ----- test 6 -----
        string t0[] = {"40", "16", "20", "13", "19", "28", "23", "3", "29", "33", "22", "14", "18", "44", "9", "12", "48", "39", "46", "41", "5", "15", "32", "47", "50", "31", "36", "11", "2", "37", "21", "6", "7", "45", "10", "30", "24", "26", "25", "4", "49", "35", "1 1", "27", "38", "42", "17", "43", "34", "8"};
        p0.assign(t0, t0 + sizeof(t0) / sizeof(t0[0]));
        p1 = -1;
        all_right = KawigiEdit_RunTest(6, p0, true, p1) && all_right;
        // ------------------
    }

    if (all_right) {
        cout << "You're a stud (at least on the example cases)!" << endl;
    } else {
        cout << "Some of the test cases had errors." << endl;
    }
    return 0;
}
// END KAWIGIEDIT TESTING
//Powered by KawigiEdit 2.1.8 (beta) modified by pivanof!
// kate: indent-mode cstyle; space-indent on; indent-width 0; 
